package com.gy.hsim.bservice.framework.mq;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import com.alibaba.fastjson.JSON;

/**
 * json转换工具类
 * @author guiyi147
 *
 */
public class JsonUtil {

	
	/**
	 * object 对象转换成 json字符串
	 * @param value
	 * @return
	 */
	public static String createJsonString(Object value) {
		String json = JSON.toJSONString(value);
		return json;
	}
	
	/**
	 * json 字符串转换成 Object
	 * @param jsonString
	 * @param cls
	 * @return
	 */
	public static <T> T getObject(String jsonString, Class<T> cls) {
		T t = null;
		try {
			t = (T) JSON.parseObject(jsonString, cls);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return t;
	}
	
	
	public  static String ConvertStream2Json(InputStream inputStream) {
		String jsonStr = "";
		
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		// 将输入流转移到内存输出流中
		try {
			while ((len = inputStream.read(buffer, 0, buffer.length)) != -1) {
				out.write(buffer, 0, len);
			}
			// 将内存流转换为字符串
			jsonStr = new String(out.toByteArray());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jsonStr;
	}

}
